package homework.test1.Controller;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import homework.test1.Domain.User;
import homework.test1.Service.LoginService;

import java.util.ArrayList;
import java.util.Scanner;

/*
//	1.使用字符流实现一个用户注册和登录功能
//		User{
//			String username;
//			int age
//			String password;
//			String phoneNum;
//			String gender;
//		}
//		每个用户信息在文件中是一行 例如:张三,18,123456,13912341234,男
 */
public class LoginController {
    private LoginService loginService = new LoginService();
    private Scanner sc = new Scanner(System.in);
    public void start(){
        while (true) {
            System.out.println("===========欢迎来到黑马管理系统===========");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.退出");
            System.out.println("请选择：");
            String choice = sc.next();
            switch (choice){
                case "1":
                    boolean flag = Login();
                    if (flag){
                        System.out.println("登录成功");
                    }
                    break;
                case "2":
                    register();
                    break;
                case "3":
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
        }
    }

    private void register() {
        //输入用户名
        System.out.println("请输入用户名");
        String name = sc.next();
        int index = loginService.isExist(name);
        if(index != -1){
            System.out.println("用户名已存在");
            return;
        }
//			String username;
//			int age
//			String password;
//			String phoneNum;
//			String gender;
//		}
        //输入年龄
        System.out.println("请输入年龄");
        int age;
        while (true) {
            try {
                String ageStr = sc.next();
                age = Integer.parseInt(ageStr);
                break;
            } catch (NumberFormatException e) {
                System.out.println("您的输入有误，请重新输入");
            }
        }
        //输入密码
        String psd;
        while (true) {
            System.out.println("请输入密码");
            psd = sc.next();
            System.out.println("请再输入一次：");
            String repsd = sc.next();
            if(repsd.equals(psd)){
                break;
            }else{
                System.out.println("您的输入有误，请重新输入");
            }
        }
        //输入手机号
        System.out.println("请输入手机号：");
        String phoneNum = sc.next();
        //输入性别
        System.out.println("请输入您的性别：");
        String gender = sc.next();

        User user = new User(name,age,psd,phoneNum,gender);
        boolean flag = loginService.addUser(user);
        if(flag){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
        ArrayList<User> list = loginService.find();
        for (User user1 : list) {
            System.out.println(user1);
        }

    }

    private boolean Login() {
        //输入用户名
        System.out.println("请输入用户名");
        String name = sc.next();
        int index = loginService.isExist(name);
        if(index == -1){
            System.out.println("用户名不存在");
            return false;
        }

        User user = loginService.getUser(index);
        //输入密码
        System.out.println("请输入密码");
        String psd = sc.next();
        if(!psd.equals(user.getPassword())){
            System.out.println("密码错误");
            return false;
        }else {
            return  true;
        }
    }
}
